//
//  HRoutersViewController.m
//  Router
//
//  Created by Zhen Huang on 2018/6/22.
//  Copyright © 2018年 James. All rights reserved.
//

#import "HRoutersViewController.h"
typedef NS_ENUM(NSInteger, RefreshDataType) {
    RefreshDataTypeHeader,
    RefreshDataTypeFooter,
};

@interface HRoutersViewController ()
<UITableViewDelegate,
UITableViewDataSource>
@property (nonatomic, strong) UITableView *tabelview;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger page_Num;
@property (nonatomic, assign) RefreshDataType orderType;
@end

@implementation HRoutersViewController
- (instancetype)init {
    if (self = [super init]) {
        self.dataArray = [NSMutableArray new];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kBackGroundColor;
    self.page_Num = 1;
    [self requestNetWorkingWith:(RefreshDataTypeHeader)];
    
    __weak typeof(HRoutersViewController*) weakSelf = self;
    self.tabelview.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        [weakSelf.tabelview.mj_header endRefreshing];
        [weakSelf.tabelview.mj_footer endRefreshing];
        [weakSelf requestNetWorkingWith:(RefreshDataTypeHeader)];
    }];
    self.tabelview.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        [weakSelf.tabelview.mj_header endRefreshing];
        [weakSelf.tabelview.mj_footer endRefreshing];
        [weakSelf requestNetWorkingWith:(RefreshDataTypeFooter)];
    }];

}
- (UITableView *)tabelview {
    if (!_tabelview) {
        _tabelview = [[UITableView alloc]initWithFrame:CGRectMake(0, NavBarHeight, ScreenWidth, ScreenHeight - NavBarHeight) style:UITableViewStyleGrouped];
        _tabelview.delegate = self;
        _tabelview.dataSource = self;
        _tabelview.backgroundColor = [UIColor whiteColor];
        _tabelview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tabelview.separatorColor = [UIColor colorWithCGColor:[kStrokeBackGroundColor CGColor]];
        [_tabelview registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CellID"];
        _tabelview.tableFooterView = [[UIView alloc]init];
    }
    return _tabelview;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 10;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 15;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc]init];
    return view;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellID" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 165;
}
- (void)requestNetWorkingWith:(RefreshDataType)type {
    
    self.orderType = type;
    if (type == RefreshDataTypeHeader) {
        self.page_Num = 1;
    }else {
        self.page_Num++;
    }
    NSString *url = F(@"%@/routers?page_size=10&page_num=%ld", BaseUrl,(long)self.page_Num);
    NSString *Token = F(@"Token %@",  [[TMCache sharedCache] objectForKey:@"Token"]);
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [self receivePOSTWithTokenURLString:url method:@"GET" body:nil Token:Token Blocks:^(NSDictionary *tempData, NSInteger state) {
            if (tempData) {
                if (type == RefreshDataTypeHeader) {
                    [self.dataArray removeAllObjects];
                }
                NSArray *resultsArray = [tempData valueForKey:@"results"];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.view addSubview:self.tabelview];
                    if (resultsArray.count < 10) {
                        [self.tabelview.mj_footer endRefreshingWithNoMoreData];
                    }else {
                        [self.tabelview.mj_footer resetNoMoreData];
                    }
                    [self.tabelview reloadData];
                });
            }
        }];
    });
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
